import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/list_empty_widget.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_button.dart';
import 'package:yt_devops/components/yt_expandable_tree/yt_expandable_tree.dart';
import 'package:yt_devops/pages/mine/user_manage/user_manager_user_detail/choose_organization_tree.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/debounce_utils.dart';
import 'mine_choose_organization_controller.dart';

class MineChooseOrganizationPage
    extends GetView<MineChooseOrganizationController> {
  const MineChooseOrganizationPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MineChooseOrganizationController>(
      builder: (_) {
        return Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBarWidget.withRightActions(
              ResKey.mineUserManagerChooseOrganization.tr),
          body: Column(
            children: [
              Container(
                height: 54,
                color: Colors.white,
                child: searchWidget(context),
              ),
              // Gaps.vGap10,
              Expanded(
                  child:
                      // Container(
                      //   color: Colors.white,
                      //   child: ChooseOrganizationTree(controller.treeData),
                      // )
                      controller.treeData.isEmpty
                          ? ListEmptyWidget(topPosition: 82)
                          : Container(
                              margin: EdgeInsets.only(
                                  top: 10.h, right: 16, left: 16,bottom: 10),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(8),
                                color: Colors.white,
                              ),
                              padding: EdgeInsets.symmetric(horizontal: 16.w),
                              child: YtExpandableTree(
                                  treeData: controller.treeData,
                                  checkedValues: controller.selectedDeviceIds,
                                  multiple: false,
                                  onValueChanged: (List nodes) {
                                    Log.d('nodes==' + nodes.toString());
                                    if (nodes.isNotEmpty) {
                                      controller.organizationIdSting =
                                          nodes[0].value.toString();
                                      controller.selectedDeviceIds = [
                                        nodes[0].value
                                      ];
                                      controller.organizationNameSting =
                                          nodes[0].title;
                                      controller.update();
                                    } else {
                                      controller.organizationIdSting = '';
                                      controller.selectedDeviceIds = [];
                                      controller.organizationNameSting = '';
                                      controller.update();
                                    }
                                  }),
                            )),
              Align(
                alignment: Alignment.bottomCenter,
                child: Container(
                  color: Colour.white,
                  // margin: EdgeInsets.only(top: 10.h),
                  padding:
                      EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
                  child: SafeArea(
                    child: YtButton(
                      enabled: controller.organizationIdSting.isNotEmpty,
                      text: ResKey.sure.tr,
                      onPress: () => controller.confirm(),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget searchWidget(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: BrnSearchText(
            maxHeight: 40,
            innerColor: Colour.backgroundColor,
            hintStyle:
                TextStyles.styleWith(16.sp, color: const Color(0xFFCCCCCC)),
            textStyle: TextStyles.f16,
            hintText: ResKey.plsEnterKeyword.tr,
            outSideColor: Colors.transparent,
            borderRadius: BorderRadius.circular(6),
            innerPadding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 0),
            onTextChange: DebounceUtils.debounceInput((p0) {
              // controller.searchName = p0;
              // controller.getSiteList();
            }),
            onTextClear: () {
              controller.searchString = '';
              controller.searchData(controller.searchString);
              return false;
            },
            onTextCommit: (p0) {
              controller.searchString = p0;
              controller.searchData(controller.searchString);
            },
          ),
        ),
      ],
    );
  }
}
